package main

import (
	"fmt"
	"time"
)
//定时器
func main() {
	//定时器 2秒后,定时器就会向自己的C通道发送一个time.Time类型的元素
	timer1 := time.NewTimer(2 * time.Second)
	t1 := time.Now()
	fmt.Printf("t1: %v\n", t1)

	t2 := <-timer1.C

	fmt.Printf("t2: %v\n", t2)

	<-time.After(2 * time.Second)
	t3 := time.Now()
	fmt.Printf("t3: %v\n", t3)

	//可以重置定时器
	timer3 := time.NewTimer(time.Second)
	go func() {
		<-timer3.C
		fmt.Println("Timer3 expired")
	}()

	stop := timer3.Stop()
	if stop {
		fmt.Println("Timer3 stopped")
	}

	fmt.Println("before")
	timer4 := time.NewTimer(5 * time.Second)
	timer4.Reset(time.Second)
	<-timer4.C
	fmt.Println("after")

}
